//
//  DataBaseHandle.m
//  UIhomework_14
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "DataBaseHandle.h"

@implementation DataBaseHandle

+ (DataBaseHandle *)shareDataBaseHandle{
    static DataBaseHandle *handle = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[DataBaseHandle alloc] init];
    });
    return handle;
}

//打开数据
- (void)openDB{
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *dbPath = [document stringByAppendingPathComponent:@"douban.sqlite"];
    NSLog(@"沙盒路径： %@", dbPath);
    
    int result = sqlite3_open(dbPath.UTF8String, &dbPoint);
    if (result == SQLITE_OK) {
        NSLog(@"打开数据成功");
    }else {
        NSLog(@"打开数据失败了");
    }
}

//创建活动表格
- (void)createTable{
    NSString *sqlStr = [NSString stringWithFormat:@"create table if not exists douban(number integer primary key autoincrement, title text)"];
    
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"建表成功");
    }else{
        NSLog(@"建表失败");
    }
}

//插入活动数据
- (void)insertModel:(TitleModel *)model{
    NSString *sqlStr = [NSString stringWithFormat:@"insert into douban(title) values('%@')", model.title];
    
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"插入数据成功");
    }else{
        NSLog(@"插入数据失败");
    }

}

//查询活动数据
- (NSMutableArray *)selectTable{
    NSString *sqlStr = [NSString stringWithFormat:@"select * from douban"];
    
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(dbPoint, sqlStr.UTF8String, -1, &stmt, NULL);
    
    NSMutableArray *arrModel = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            
            TitleModel *model = [TitleModel titleModel];
            model.title = [NSString stringWithUTF8String:(const char *)title];
            [arrModel addObject:model];
            
        }
        NSLog(@"查询成功");
    }else{
        NSLog(@"查询失败");
    }
    return arrModel;
}

//删除活动数据
- (void)deleteModel:(TitleModel *)model{
    NSString *sqlStr = [NSString stringWithFormat:@"delete from douban"];
    
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
}



//创建电影表格
- (void)createMovieTable{
    NSString *sqlStr = [NSString stringWithFormat:@"create table if not exists Movie(number integer primary key autoincrement, title text)"];
    
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"创建电影表格成功");
    }else {
        NSLog(@"创建电影表格失败");
    }
}

//插入电影数据
- (void)insertMovieModel:(MovieModel *)model{
    NSString *sqlStr = [NSString stringWithFormat:@"insert into Movie(title) values('%@')", model.title];
    
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"插入电影数据成功");
    }else{
        NSLog(@"插入电影数据失败");
    }
}

//查询电影数据
-(NSMutableArray *)selectMovieTable{
    NSString *sqlStr = [NSString stringWithFormat:@"select * from movie"];
    
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(dbPoint, sqlStr.UTF8String, -1, &stmt, NULL);
    
    NSMutableArray *arrModel = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt)  == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            
        MovieModel *model = [MovieModel movieModel];
            model.title = [NSString stringWithUTF8String:(const char *)title];
        [arrModel addObject:model];
            
        }
    }
    return arrModel;
 }

//删除电影数据
- (void)deleteMovieModel:(MovieModel *)model{
    NSString *sqlStr = [NSString stringWithFormat:@"delete from movie"];
    
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删除电影数据成功");
    }else{
        NSLog(@"删除电影数据失败");
    }
}



//创建注册表格
- (void)createRegister{
    NSString *sqlStr = [NSString stringWithFormat:@"create table if not exists Register(username text primary key, password text, againPassword text, mailbox text, phone text)"];
    
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"创建表格成功");
    }else{
        NSLog(@"创建表格失败");
    }
}

//插入注册数据
- (void)insertRegister:(RegisterModel *)model{
    NSString *sqlStr = [NSString stringWithFormat:@"insert into Register(username, password, againPassword, mailbox, phone) values('%@', '%@', '%@', '%@', '%@')", model.username, model.password, model.againPassword, model.mailbox, model.phone];
    
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"插入注册数据成功");
    }else{
        NSLog(@"插入注册数据失败");
    }
}

//查询注册数据
- (NSMutableArray *)selectRegister{
    NSString *sqlStr = [NSString stringWithFormat:@"select * from register"];
    
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(dbPoint, sqlStr.UTF8String, -1, &stmt, NULL);
    
    NSMutableArray *arrModel = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *username = sqlite3_column_text(stmt, 0);
            const unsigned char *password = sqlite3_column_text(stmt, 1);
            const unsigned char *againPassword = sqlite3_column_text(stmt, 2);
            const unsigned char *mailbox = sqlite3_column_text(stmt, 3);
            const unsigned char *phone = sqlite3_column_text(stmt, 4);
            
            
            
            RegisterModel *model = [RegisterModel registerModel];
            model.username = [NSString stringWithUTF8String:(const char *)username];
            model.password = [NSString stringWithUTF8String:(const char *)password];
            model.againPassword = [NSString stringWithUTF8String:(const char *)againPassword];
            model.mailbox = [NSString stringWithUTF8String:(const char *)mailbox];
            model.phone = [NSString stringWithUTF8String:(const char *)phone];
            [arrModel addObject:model];
        }
    }else {
        NSLog(@"查询注册数据失败");
    }
    return arrModel;
}
@end
